package com.als.healthymananger.controller.impl;

import com.als.healthymananger.controller.DietsApi;
import com.als.healthymananger.entity.Diet;
import com.als.healthymananger.service.DietsService;
import com.als.healthymananger.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/lib/diet")
public class DietController implements DietsApi {
    @Autowired
    private DietsService dietsService;

    @Override
    public RespBean createDiets(Diet diet) {
        return dietsService.save(diet)? RespBean.ok("添加成功"): RespBean.badRequest("添加失败");
    }

    @Override
    public RespBean deleteDiets(int dietId) {
        return dietsService.removeById(dietId)? RespBean.ok("删除成功"): RespBean.badRequest("删除失败");
    }

    @Override
    public RespBean updateDiets(int dietId, Diet diet) {
        return dietsService.updateById(diet)? RespBean.ok("修改成功"): RespBean.badRequest("修改失败");
    }

    @Override
    public RespBean getDiets(int dietId) {
        Diet diet = dietsService.getById(dietId);
        return diet == null? RespBean.badRequest("获取失败"): RespBean.ok("获取成功", diet);
    }
}
